#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>

#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include "test_redis_get.h"

#define LOCAL_IP     "192.168.2.76"
#define LOCAL_PORT   9000
#define BUF_SIZE     1024

/** 共享内存块和 */
#define SHM_NAME "train_share_mem"
#define SHM_SIZE 4096


typedef struct {
    uint8_t  TID;       // TID_MVB_PD_DATA
    uint8_t  fcode;     // 端口字节大小 0,1,2,3,4
    uint16_t pdPort;    // 端口号
    uint8_t  pdData[32];// 数据区
} mvb_data_full;

#define MVB_HEADER_0 0x21
#define MVB_HEADER_1 0x04
#define HEADER_SIZE  (sizeof(uint8_t) * 2)           // TID + fcode
#define ADDR_SIZE    (sizeof(uint16_t))              // pdPort
#define LEN_SIZE     (sizeof(mvb_data_full))         // 整个数据结构长度
#define CRC_SIZE     (sizeof(uint16_t))              // 假设CRC为2字节



// 定义数据类型枚举
typedef enum {
    DATA_TYPE_WEIGHT = 0,         // 车载重量数据
    DATA_TYPE_CLOCK,              // 时钟数据
    DATA_TYPE_AIR_CONSUMPTION,    // 空调能耗数据
    DATA_TYPE_A1_TEMP_HUMI,       // A1车厢温湿度数据
    DATA_TYPE_B1_TEMP_HUMI,       // B1车厢温湿度数据
    DATA_TYPE_B2_TEMP_HUMI,       // B2车厢温湿度数据
    DATA_TYPE_A2_TEMP_HUMI,       // A2车厢温湿度数据
    DATA_TYPE_A1_VALVE,           // A1车厢阀门状态
    DATA_TYPE_B1_VALVE,           // B1车厢阀门状态
    DATA_TYPE_B2_VALVE,           // B2车厢阀门状态
    DATA_TYPE_A2_VALVE,           // A2车厢阀门状态
    DATA_TYPE_UNKNOWN             // 未知数据类型
} DataType;

// 车载重量结构体，包含A1, B1, B2, A2重量和节能状态
typedef struct {
    uint16_t a1_weight;
    uint16_t b1_weight;
    uint16_t b2_weight;
    uint16_t a2_weight;
    uint8_t  is_energy_saving; // 0: 否, 1: 是
} WeightData;

// 时钟结构体，年、月、日、时、分、秒分别用uint8_t存储
typedef struct {
    uint8_t year;   // 年
    uint8_t month;  // 月
    uint8_t day;    // 日
    uint8_t hour;   // 时
    uint8_t minute; // 分
    uint8_t second; // 秒
    uint8_t reserved1; // 保留
    uint8_t reserved2; // 保留
} ClockData;

// 空调能耗结构体，包含司机室、A1、B1、B2、A2、尾端司机室、总能耗
typedef struct {
    float driver_cab_consumption;      // 司机室能耗
    float a1_consumption;              // A1能耗
    float b1_consumption;              // B1能耗
    float b2_consumption;              // B2能耗
    float a2_consumption;              // A2能耗
    float tail_driver_cab_consumption; // 尾端司机室能耗
    float total_consumption;           // 空调总能耗
} AirConsumptionData;

// 列车温湿度结构体，包含室外温度、客室湿度、客室PM2.5、客室PMTOVC、客室二氧化碳浓度
typedef struct {
    uint8_t outdoor_temp;      // 室外温度
    uint8_t room_humidity;     // 客室湿度
    uint16_t room_pm25;        // 客室PM2.5
    uint16_t room_pmtovc;      // 客室PMTOVC
    uint16_t room_co2;         // 客室二氧化碳浓度
} TempHumiData;

// 控制阀数据结构体，包含客室温度、回风温度、新风温度，以及16个新风阀开度状态位
typedef struct {
    uint8_t room_temp;         // 客室温度
    uint8_t reserved0; // 保留一个uint8_t的空位置
    uint8_t return_air_temp;   // 客室回风温度
    uint8_t reserved1; // 保留一个uint8_t的空位置
    uint8_t fresh_air_temp;    // 客室新风温度
    uint8_t reserved2; // 保留一个uint8_t的空位置
    uint8_t group_1;
    uint8_t group_2;
    uint16_t reserved3; // 16个新风阀开度状态位，每一位代表一个阀门的开关状态
    uint16_t reserved4; // 16个新风阀开度状态位，每一位代表一个阀门的开关状态
    uint16_t fresh_valve_status; // 16个新风阀开度状态位，每一位代表一个阀门的开关状态
    uint16_t fresh_valve_status2;
} ValveData;

// 定义全局数据结构
typedef struct {
    WeightData weight;                 // 车载重量
    ClockData clock;                   // 时钟
    AirConsumptionData air_consumption;// 空调能耗
    TempHumiData a1_temp_humi;         // A1温湿度
    TempHumiData b1_temp_humi;         // B1温湿度
    TempHumiData b2_temp_humi;         // B2温湿度
    TempHumiData a2_temp_humi;         // A2温湿度
    ValveData a1_valve;                // A1控制阀数据
    ValveData b1_valve;                // B1控制阀数据
    ValveData b2_valve;                // B2控制阀数据
    ValveData a2_valve;                // A2控制阀数据
} TrainData;

// 全局变量
TrainData g_train_data;



//这是是UDP 传递给 低碳平台的数据 start
typedef struct {
    uint16_t datatype; // 1公共数据， 2 单个列车数据  3 控制发送数据
    uint16_t blank; // 列车ID
    uint16_t datetime;  //时间戳
    uint16_t mode; //是否节能模式 0 非节能， 1 节能
    AirConsumptionData air_consumption;
    WeightData weight;
} Global_Train_Data;

//低碳平台数据
Global_Train_Data global_train_data;

typedef struct {
    uint8_t target_temp; // 目标温度
    uint8_t driver_temp; // 司机室目标温度

    uint8_t passenger_temp; // 乘客室回风温度
    uint8_t driver_return_temp; // 司机室回风温度

    uint8_t passenger_new_temp; // 乘客室新风温度
    uint8_t driver_new_temp; // 司机室新风温度

    uint8_t blank1;
    uint8_t blank2;
    uint16_t mode_data;  //模式数据
    uint16_t enpressor_data;  //压缩机数据
    uint16_t new_wind_data;  //新风开度数据
    uint16_t new_wind_data2;  //新风开度数据2
    uint16_t blank3;  //空位
    uint16_t blank4;  //空位
    uint16_t blank5;  //空位
    uint16_t blank6;  //空位
    uint16_t blank7;  //空位
} Single_data_item;
//
typedef struct {
    uint16_t datatype; // 1公共数据， 2 单个列车数据  3 控制发送数据
    uint16_t sn; // 列车代号
    uint16_t datetime;  //时间戳
    Single_data_item single_data;
} Single_Train_Data;

//单台车辆的实时数据
Single_Train_Data single_train_data_a1;
Single_Train_Data single_train_data_b1;
Single_Train_Data single_train_data_b2;
Single_Train_Data single_train_data_a2;

typedef struct {
    uint8_t datatype; // 1公共数据， 2 单个列车数据  3 控制发送数据
    uint8_t sn; // 列车代号
    uint32_t datetime;  //时间戳

    uint8_t target_temp; //客室弥补表温度
    uint8_t blank;

    uint16_t blank3;  //空位
    uint16_t blank4;  //空位
    uint16_t blank5;  //空位
    uint16_t blank6;  //空位
    uint16_t blank7;  //空位

    uint8_t new_wind_data; //新风阀开度指令
    uint8_t blank13;

    uint16_t blank8;  //空位
    uint16_t blank9;  //空位
    uint16_t blank10;  //空位
    uint16_t blank11;  //空位
    uint16_t blank12;  //空位
} Single_Train_Order_Data;

//下发给每个车辆的指令数据
Single_Train_Order_Data single_train_order_data;

//这是是UDP 传递给 低碳平台的数据  end



// 将redisContext* c 定义为全局变量
redisContext* redis_client = NULL;



// 写操作函数
void write_train_data(DataType type, void *data)
{
    switch (type) {
        case DATA_TYPE_WEIGHT: // 车载重量
            memcpy(&g_train_data.weight, data, sizeof(WeightData));
            memcpy(&global_train_data.weight, data, sizeof(WeightData));
            break;
        case DATA_TYPE_CLOCK: // 时钟
            memcpy(&g_train_data.clock, data, sizeof(ClockData));
            printf("g_train_data.clock: year=%u, month=%u, day=%u, hour=%u, minute=%u, second=%u\n",
                   g_train_data.clock.year,
                   g_train_data.clock.month,
                   g_train_data.clock.day,
                   g_train_data.clock.hour,
                   g_train_data.clock.minute,
                   g_train_data.clock.second);
            break;
        case DATA_TYPE_AIR_CONSUMPTION: // 空调能耗
            memcpy(&g_train_data.air_consumption, data, sizeof(AirConsumptionData));
            memcpy(&global_train_data.air_consumption, data, sizeof(AirConsumptionData));
            // 以十六进制形式输出 g_train_data.air_consumption 的内容
            
            break;
        case DATA_TYPE_A1_TEMP_HUMI: // A1温湿度
            memcpy(&g_train_data.a1_temp_humi, data, sizeof(TempHumiData));
            break;
        case DATA_TYPE_B1_TEMP_HUMI: // B1温湿度
            memcpy(&g_train_data.b1_temp_humi, data, sizeof(TempHumiData));
            break;
        case DATA_TYPE_B2_TEMP_HUMI: // B2温湿度
            memcpy(&g_train_data.b2_temp_humi, data, sizeof(TempHumiData));
            break;
        case DATA_TYPE_A2_TEMP_HUMI: // A2温湿度
            memcpy(&g_train_data.a2_temp_humi, data, sizeof(TempHumiData));
            break;
        case DATA_TYPE_A1_VALVE: // A1控制阀
            memcpy(&g_train_data.a1_valve, data, sizeof(ValveData));
            break;
        case DATA_TYPE_B1_VALVE: // B1控制阀
            memcpy(&g_train_data.b1_valve, data, sizeof(ValveData));
            break;
        case DATA_TYPE_B2_VALVE: // B2控制阀
            memcpy(&g_train_data.b2_valve, data, sizeof(ValveData));
            break;
        case DATA_TYPE_A2_VALVE: // A2控制阀
            memcpy(&g_train_data.a2_valve, data, sizeof(ValveData));
            break;
        default:
            // 未知类型，不做处理
            break;
    }

    // 以十六进制形式输出 g_train_data 的内容
    unsigned char *ptr = (unsigned char *)&g_train_data;
    size_t len = sizeof(g_train_data);
    printf("g_train_data hex dump (%zu bytes):\n", len);
    for (size_t i = 0; i < len; ++i) {
        printf("%02X ", ptr[i]);
        if ((i + 1) % 16 == 0)
            printf("\n");
    }
    if (len % 16 != 0) {
        printf("\n");
    }
}

// 获取当前时间戳，并转换为16进制的uint16_t格式返回
uint32_t get_current_time_uint32() {
    time_t now = time(NULL);
    // 直接返回当前时间戳，已uint32_t格式返回
    return (uint32_t)now;
}


uint8_t parseTemp(int temp) {
    // 检查温度是否在14~40度之间
    if (temp < 14 || temp > 40) {
        return 0x10; // 16的十六进制
    }
    return (uint8_t)(temp & 0xFF); // 返回温度的低8位（十六进制格式）
}


// 根据输入的百分比（0, 25, 50, 75, 100），返回对应的格式化二进制uint8_t
uint8_t get_valve_binary(int percent) {
    switch (percent) {
        case 0:
            return 0x01; // 全关 00000001
        case 25:
            return 0x02; // 开25% 00000010
        case 50:
            return 0x04; // 开50% 00000100
        case 75:
            return 0x08; // 开75% 00001000
        case 100:
            return 0x10; // 全开 00010000
        default:
            return 0x00; // 非法输入，返回0
    }
}


//此函数根据不同的端口， 映射不同的数据结构， 并得到的列车参数内容
void port_action(uint16_t pdPort_be, uint8_t *data)
{
    switch (pdPort_be) {
        case 249:
            printf("pdPort_be = 【车载重量】\n");
            // 取data的第9至第16个字节（即data[8]到data[15]），传入write_train_data
            uint8_t weight_data_segment[8];
            memcpy(weight_data_segment, data + 8, 8);
            write_train_data(DATA_TYPE_WEIGHT, weight_data_segment);
            break;
        case 255:
            // 取data的第1至第6个字节（即data[0]到data[5]），传入write_train_data
            uint8_t clock_data_segment[6];
            memcpy(clock_data_segment, data, 6);
            write_train_data(DATA_TYPE_CLOCK, clock_data_segment);
            break;
        case 39:
            uint8_t ac_energy_data_segment[28];
            memcpy(ac_energy_data_segment, data, 28);
            write_train_data(DATA_TYPE_AIR_CONSUMPTION, ac_energy_data_segment);
            printf("pdPort_be = 【空调能耗】\n");
            break;
        case 401:
            uint8_t a1_temp_humidity_data_segment[8];
            memcpy(a1_temp_humidity_data_segment, data + 4, 8);
            write_train_data(DATA_TYPE_A1_TEMP_HUMI, a1_temp_humidity_data_segment);
            printf("pdPort_be = 【列车A1 温湿度数据】\n");
            break;
        case 657:
            uint8_t b1_temp_humidity_data_segment[8];
            memcpy(b1_temp_humidity_data_segment, data + 4, 8);
            write_train_data(DATA_TYPE_B1_TEMP_HUMI, b1_temp_humidity_data_segment);
            printf("pdPort_be = 【列车B1温湿度数据】\n");
            break;
        case 913:
            uint8_t b2_temp_humidity_data_segment[8];
            memcpy(b2_temp_humidity_data_segment, data + 4, 8);
            write_train_data(DATA_TYPE_B2_TEMP_HUMI, b2_temp_humidity_data_segment);
            printf("pdPort_be = 【列车B2温湿度数据】\n");
            break;
        case 1169:
            uint8_t a2_temp_humidity_data_segment[8];
            memcpy(a2_temp_humidity_data_segment, data + 4, 8);
            write_train_data(DATA_TYPE_A2_TEMP_HUMI, a2_temp_humidity_data_segment);
            printf("pdPort_be = 【列车A2温湿度数据】\n");
            break;
        case 400:
            uint8_t a1_valve_data_segment[16];
            memcpy(a1_valve_data_segment, data + 2, 16);
            write_train_data(DATA_TYPE_A1_VALVE, a1_valve_data_segment);
            single_train_data_a1.datatype = 0x02;
            single_train_data_a1.sn = 0xA1;
            single_train_data_a1.datetime = get_current_time_uint32();
            memcpy(&single_train_data_a1.single_data, data + 3, sizeof(Single_data_item));
            printf("pdPort_be = 【列车A1 控制阀数据】\n");
            break;
        case 656:
            uint8_t b1_valve_data_segment[16];
            memcpy(b1_valve_data_segment, data + 2, 16);
            write_train_data(DATA_TYPE_B1_VALVE, b1_valve_data_segment);
            single_train_data_b1.datatype = 0x02;
            single_train_data_b1.sn = 0xB1;
            single_train_data_b1.datetime = get_current_time_uint32();
            memcpy(&single_train_data_b1.single_data, data + 3, sizeof(Single_data_item));
            printf("pdPort_be = 【列车B1 控制阀数据】\n");
            break;
        case 912:
            uint8_t b2_valve_data_segment[16];
            memcpy(b2_valve_data_segment, data + 2, 16);
            write_train_data(DATA_TYPE_B2_VALVE, b2_valve_data_segment);
            single_train_data_b2.datatype = 0x02;
            single_train_data_b2.sn = 0xB2;
            single_train_data_b2.datetime = get_current_time_uint32();
            memcpy(&single_train_data_b2.single_data, data + 3, sizeof(Single_data_item));
            printf("pdPort_be = 【列车B2 控制阀数据】\n");
            break;
        case 1168:
            uint8_t a2_valve_data_segment[16];
            memcpy(a2_valve_data_segment, data + 2, 16);
            write_train_data(DATA_TYPE_A2_VALVE, a2_valve_data_segment);
            single_train_data_a2.datatype = 0x02;
            single_train_data_a2.sn = 0xA2;
            single_train_data_a2.datetime = get_current_time_uint32();
            memcpy(&single_train_data_a2.single_data, data + 3, sizeof(Single_data_item));
            printf("pdPort_be = 【列车A2 控制阀数据】\n");
            break;
        default:
            printf("pdPort_be = %d\n", pdPort_be);
    }

    // 将 single_train_data_a2 按照十六进制输出
    unsigned char *ptr = (unsigned char *)&single_train_data_a2;
    size_t len = sizeof(single_train_data_a2);
    printf("single_train_data_a2 hex: ");
    for (size_t i = 0; i < len; ++i) {
        printf("%02X ", ptr[i]);
    }

    // INSERT_YOUR_CODE
    // 将 ptr 转换为 string
    char ptr_hex_str[sizeof(single_train_data_a2) * 2 + 1] = {0};
    for (size_t i = 0; i < len; ++i) {
        sprintf(ptr_hex_str + i * 2, "%02X", ptr[i]);
    }
    redis_rpush(redis_client, "mylistlist", ptr_hex_str);
    printf("\n");


    //检查是否满足控温条件
      // 满足条件则发送控制指令

    
    //下发控制指令

    int arr[4];
    arr[0] = 3344;
    arr[1] = 3360;
    arr[2] = 3376;
    arr[3] = 3392;

    int temp = 29;
        // INSERT_YOUR_CODE
        for (int i = 0; i < 4; i++) {
            int pdPort_be = arr[i];

            // 构造发送数据结构
            mvb_data_full send_data;
            memset(&send_data, 0, sizeof(send_data));
            send_data.TID = 0x21;      // 示例TID
            send_data.fcode = 0x04;    // 示例fcode
            send_data.pdPort = htons(pdPort_be);

            // 这里可以根据实际需要填充 send_data.pdData
            // 例如全部置零或填充特定指令

            // INSERT_YOUR_CODE
            // 生成16~25之间的随机数赋值给temp
            temp = (rand() % 10) + 16;
            send_data.pdData[4] = parseTemp(temp); // 示例

            int percent;
            // INSERT_YOUR_CODE
            // 生成0, 20, 50, 75, 100中随机一个
            int percent_options[] = {0, 20, 50, 75, 100};
            int idx = rand() % 5;
            percent = percent_options[idx];
            send_data.pdData[16] = get_valve_binary(percent);

            // 发送UDP数据
            int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            if (sockfd < 0) {
                perror("socket creation failed");
                continue;
            }

            struct sockaddr_in dest_addr;
            memset(&dest_addr, 0, sizeof(dest_addr));
            dest_addr.sin_family = AF_INET;
            dest_addr.sin_addr.s_addr = inet_addr("192.168.2.200"); // 目标IP，可根据实际情况修改
            dest_addr.sin_port = htons(8000); // 目标端口，可根据实际情况修改

            ssize_t sent = sendto(sockfd, &send_data, sizeof(send_data), 0,
                                (struct sockaddr*)&dest_addr, sizeof(dest_addr));


            // INSERT_YOUR_CODE
            // 输出 send_data 的十六进制内容
            unsigned char *data_ptr = (unsigned char *)&send_data;
            size_t data_len = sizeof(send_data);
            printf("send_data hex: ");
            for (size_t j = 0; j < data_len; ++j) {
                printf("%02X ", data_ptr[j]);
            }
            printf("\n");

            
            if (sent < 0) {
                perror("sendto failed");
            } else {
                printf("已发送控制指令到端口0x%04X, 字节数: %zd\n", pdPort_be, sent);
            }
            close(sockfd);
        }
}




int main() {



    // 从config.conf读取redis_ip和redis_port
    char redis_ip[128] = "127.0.0.1";
    int redis_port = 6379;
    FILE* conf = fopen("config.conf", "r");
    if (conf) {
        char line[256];
        while (fgets(line, sizeof(line), conf)) {
            if (strncmp(line, "redis_ip=", 9) == 0) {
                sscanf(line + 9, "%127s", redis_ip);
            } else if (strncmp(line, "redis_port=", 11) == 0) {
                sscanf(line + 11, "%d", &redis_port);
            }
        }
        fclose(conf);
    }
    redis_client = redis_connect(redis_ip, redis_port);
    if (!redis_client) return -1;



    int sockfd;
    struct sockaddr_in localaddr, remoteaddr;
    char buffer[BUF_SIZE];
    socklen_t addr_len = sizeof(remoteaddr);
    ssize_t n;

    // 创建UDP套接字
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&localaddr, 0, sizeof(localaddr));
    localaddr.sin_family = AF_INET;
    localaddr.sin_addr.s_addr = inet_addr(LOCAL_IP);
    localaddr.sin_port = htons(LOCAL_PORT);
    if (bind(sockfd, (struct sockaddr*)&localaddr, sizeof(localaddr)) < 0) {
        perror("bind error");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    printf("等待接收MVB协议UDP数据...\n");
    while (1) {
    
        n = recvfrom(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr*)&remoteaddr, &addr_len);
        if (n < 0) {
            perror("recvfrom error");
            break;
        }

        // 将接收到的数据映射为 mvb_data_full 结构体
        printf("mvb_data_full 结构体大小: %zu\n", sizeof(mvb_data_full));
        
        // mvb_data_full *mvb_data = (mvb_data_full *)buffer;

        mvb_data_full mvb_data_zero = {0};
        memcpy(&mvb_data_zero, buffer, sizeof(mvb_data_full));
        mvb_data_full *mvb_data = &mvb_data_zero;

         for (ssize_t i = 0; i < sizeof(mvb_data_full); ++i) {
            printf("%02X ", (unsigned char)buffer[i]);
        }
        
        // 你可以在这里访问 mvb_data 的各个字段，例如:
        // 修正：假设 mvb_data_full 结构体中 pdPort 字段不是 header 指针下的成员，而是直接成员
        // 正确输出 pdPort 的数值，假设 pdPort 是 uint16_t 类型，避免类型提升导致的错误

        uint16_t pdPort_be = ((uint8_t*)&mvb_data->pdPort)[0] << 8 | ((uint8_t*)&mvb_data->pdPort)[1];

        printf("mvb_data->pdPort (10进制) = %d\n", pdPort_be);
        printf("mvb_data->pdPort (十六进制): ");
        // 假设 pdPort 是一个非数组类型（如 uint16_t 或 uint32_t），逐字节打印
        char pdPort_hex[sizeof(mvb_data->pdPort) * 2 + 1] = {0};
        for (size_t i = 0; i < sizeof(mvb_data->pdPort); ++i) {
            sprintf(&pdPort_hex[i * 2], "%02X", ((unsigned char*)&mvb_data->pdPort)[i]);
        }
        printf("%s\n", pdPort_hex);
        
        // 优化switch：用结构体数组映射pdPort_be到描述，查找更高效，便于维护
        typedef struct {
            uint16_t port;
            const char *desc;
        } PdPortDesc;

        static const PdPortDesc pdPortDescTable[] = {
            {0x00F9, "【车载重量】"},
            {0x00FF, "【时钟】"},
            {0x0027, "【空调能耗】"},
            {0x0191, "【列车温湿度数据】"},
            {0x0291, "【列车温湿度数据】"},
            {0x0391, "【列车温湿度数据】"},
            {0x0491, "【列车温湿度数据】"},
            {0x0190, "【列车数据】"},
            {0x0290, "【列车数据】"},
            {0x0390, "【列车数据】"},
            {0x0490, "【列车数据】"},
        };

        const char *desc = NULL;
        for (size_t i = 0; i < sizeof(pdPortDescTable)/sizeof(pdPortDescTable[0]); ++i) {
            if (pdPort_be == pdPortDescTable[i].port) {
                desc = pdPortDescTable[i].desc;
                
                //执行端口数据处理动作
                port_action(pdPort_be, mvb_data->pdData);
                break;
            }
        }

        if (desc) {
            printf("pdPort_be = %s\n", desc);
        } else {
            printf("pdPort_be = %d\n", pdPort_be);
        }

        //读取的到数据 映射到 中心数据集合 ，条件当前的情况组合
            //持续时长
            //温度条件
            //湿度条件
            //重量条件
            //是否紧急模式
            //空调能耗条件
            //列车数据条件
            //列车温湿度数据条件
            //列车数据条件

        //异步脚本来试试
            //存储到同一个内存区块
            //异步数据调整，流数据处理更新
            //工况温判定
            //工况指令下发

        
        //数据封装下发，下发到低碳节能平台， 平台下发到设备
            //上行发送数据
            //下行存储数据 状态判定

        printf("\n");

        //端口重置 
        pdPort_be   = 0x0000;
        //释放buffer 空间后续使用
        memset(buffer, 0, sizeof(buffer));
    }
    close(sockfd);
    
    return 0;
}